package com.sxc.study.tts;

import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.sxc.study.R;

import java.util.Locale;

public class TTSActivity extends AppCompatActivity {

    private static final String TAG = TTSActivity.class.getSimpleName();
    private EditText speak_content;
    private Button speak;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tts);
        speak = findViewById(R.id.speak);
        speak_content = findViewById(R.id.speak_content);

        final TextToSpeech textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                Log.d(TAG, "onInit: " + status);
                if (status == TextToSpeech.SUCCESS) {
                    Log.d(TAG, "onInit: success");
                }
            }
        });

        textToSpeech.setLanguage(Locale.CHINA);

        speak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = speak_content.getText().toString();
                if (TextUtils.isEmpty(content))
                    return;
                textToSpeech.speak(content, TextToSpeech.QUEUE_FLUSH, null);
            }
        });
    }
}
